#coding=utf-8
from uliweb.orm import *
from uliweb.utils.common import get_var

def get_modified_user():
    from uliweb import request
    
    return request.user.id

class Plan(Model):    
    __verbose_name__ = u'计划'

    task = Reference('tasks_develop', verbose_name="开发任务", collection_name='tasks_plans', index=True)
    title = Field(str, verbose_name="名称", max_length=40, required=True)
    type = Field(CHAR, max_length=1, verbose_name="计划类型", choices=get_var('PARA/PLAN_TYPE'), required=True)
    begin_date = Field(datetime.date, verbose_name="计划开始时间")
    end_date = Field(datetime.date, verbose_name="计划结束时间")
    test_type = Field(bool, verbose_name="测试相关")
    memo = Field(TEXT, verbose_name="备注")
    idn = Field(str, verbose_name="标识信息", max_length=4)
    main_sys = Reference('system', verbose_name="计划系统", collection_name='system_plans')
    systems = ManyToMany('system', verbose_name="执行系统", collection_name='systems_plans')
    is_public = Field(bool, verbose_name="是否公共")
    is_reserved = Field(bool, verbose_name="是否保留")
    stage = Field(CHAR, verbose_name='所处阶段',max_length=2,required=True, choices=get_var('PARA/TASK_STAGE'), default='00')
    weight = Field(float, verbose_name="权重", default=0.1)
    responsible = Field(str, max_length=200, verbose_name="责任人")
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    
    def __unicode__(self):
        return self.title
    
class PlanEdit(Model):    
    __verbose_name__ = u'计划编辑'

    task = Reference('tasks_develop', verbose_name="开发任务", collection_name='tasks_planedits', index=True)
    title = Field(str, verbose_name="名称", max_length=40, required=True)
    type = Field(CHAR, max_length=1, verbose_name="计划类型", choices=get_var('PARA/PLAN_TYPE'), required=True)
    begin_date = Field(datetime.date, verbose_name="计划开始时间")
    end_date = Field(datetime.date, verbose_name="计划结束时间")
    test_type = Field(bool, verbose_name="测试相关")
    memo = Field(TEXT, verbose_name="备注")
    idn = Field(str, verbose_name="标识信息", max_length=4)
    main_sys = Reference('system', verbose_name="计划系统", collection_name='system_planedits')
    systems = ManyToMany('system', verbose_name="关联系统", collection_name='systems_planedits')
    is_public = Field(bool, verbose_name="是否公共", default=False)
    is_reserved = Field(bool, verbose_name="是否保留")
    enabled = Field(bool, verbose_name="是否启用", default=False)
    stage = Field(CHAR, verbose_name='所处阶段',max_length=2,required=True, choices=get_var('PARA/TASK_STAGE'), default='00')
    weight = Field(float, verbose_name="权重", default=0)
    responsible = Field(str, max_length=200, verbose_name="责任人")
    
    @classmethod
    def OnInit(cls):
        Index('planedit_indx', cls.c.task, cls.c.idn, unique=True)
    
class PlanExecute(Model):
    __verbose_name__ = u'计划执行'
    
    task = Reference('tasks_develop', verbose_name="开发任务", collection_name='tasks_planexecutes')
    plan = Reference('plan', verbose_name="计划", collection_name="plan_executes")
    type = Field(CHAR, max_length=1, verbose_name="计划类型", choices=get_var('PARA/PLAN_TYPE'), required=True)
    system = Reference('system', verbose_name="执行系统")
    finish_begin_date = Field(datetime.date, verbose_name="实际开始时间")
    finish_end_date = Field(datetime.date, verbose_name="实际结束时间")
    status = Field(CHAR, verbose_name="执行状态", max_length=1, required=True, default='1', choices=get_var('PARA/PLAN_STATUS'))
    memo = Field(TEXT, verbose_name="备注")
    submission = Field(FILE, verbose_name='提交件')
    delay_classes = Field(CHAR, max_length=2, verbose_name='延迟原因分类', choices=get_var('PARA/TASK_DELAY_CLASSES'))
    delay_reason = Field(TEXT, verbose_name='延迟原因说明')
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    begin_date = Field(datetime.date, verbose_name="计划开始时间")
    end_date = Field(datetime.date, verbose_name="计划结束时间")
    sync_date = Field(datetime.date, verbose_name="上次更新日期")
    desc = Field(TEXT, verbose_name='完成情况说明')
    executer = Field(str, verbose_name='执行人', max_length=80)
    finish = Field(float, verbose_name='实际完成进度', default=0.0)
    testplan = Reference('testplan', verbose_name='测试计划', collection_name='testplan_taskexecutes')
    
    @classmethod
    def OnInit(cls):
        Index('planexecute_indx', cls.c.task, cls.c.plan, cls.c.system, unique=True)
        
    def __unicode__(self):
        return '%s-%s' % (unicode(self.task), unicode(self.plan))
    
class PlanExecuteDetails(Model):
    __verbose_name__ = u'计划执行明细'
    
    begin_date = Field(datetime.date, verbose_name='开始时间')
    end_date = Field(datetime.date, verbose_name='结束时间')
    task = Reference('tasks_develop', verbose_name="开发任务", collection_name='task_planexecutedetails')
    plan = Reference('plan', verbose_name="计划")
    planexecute = Reference('planexecute', verbose_name='计划执行')
    system = Reference('system', verbose_name="执行系统")
    desc = Field(TEXT, verbose_name='完成情况说明')
    executer = Field(str, verbose_name='执行人', max_length=80)
    finish = Field(float, verbose_name='实际完成进度')
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', auto=True, auto_add=True, default=get_modified_user)

    @classmethod
    def OnInit(cls):
        Index('ped_indx', cls.c.begin_date, cls.c.end_date, cls.c.task, cls.c.plan, cls.c.system, unique=True)
    
    class EditForm:
        fields = ['executer', 'finish', 'desc']
    
class PlanChange(Model):
    __verbose_name__ = u'计划修改'
    
    task_id = Field(int, verbose_name="任务ID", required=True)
    plan_id = Field(int, verbose_name="计划ID", required=True)
    modify_flag = Field(CHAR, max_length=1, verbose_name="修改标志", choices=get_var('PARA/AUDIT_MODIFY_FLAG'))
    old_value = Field(BLOB, verbose_name="原内容")
    changed_value = Field(BLOB, verbose_name="修改内容")
    idn = Field(str, verbose_name="标识信息", max_length=4)
    weight = Field(float, verbose_name="权重", default=0)
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', collection_name='modifier_planchanges', default=get_modified_user)
    
